package com.hfut.manage.controller;


import com.hfut.manage.pojo.Department;
import com.hfut.manage.pojo.Result;
import com.hfut.manage.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zwh
 * @since 2025-06-03
 */
@RestController
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    public Result list(@RequestParam(defaultValue = "") String name) {
        List<Department> departmentList = departmentService.lambdaQuery()
                .like(Department::getName, name)
                .list();
        return Result.success(departmentList);
    }

    @PostMapping
    public Result save(@RequestBody Department department) {
        boolean success = departmentService.save(department);
        if (!success) {
            return Result.error(500, "新增部门失败");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Long id) {
        boolean success = departmentService.removeById(id);
        if (!success) {
            return Result.error(500, "删除部门失败");
        }
        return Result.success();
    }
}
